<template>
  <el-form ref="form" :model="form" label-width="60px">
    <el-form-item label="用户名">
      <el-input v-model="form.username"></el-input>
    </el-form-item>
    <el-form-item label="密码">
      <el-input type="password" v-model="form.password"></el-input>
    </el-form-item>
    <el-form-item label="学号">
      <el-input v-model="form.studentNo"></el-input>
    </el-form-item>
    <el-form-item label="姓名">
      <el-input v-model="form.studentName"></el-input>
    </el-form-item>
    <el-form-item label="性别">
      <el-select v-model="form.gender">
        <el-option value="男">男</el-option>
        <el-option value="女">女</el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="邮箱">
      <el-input v-model="form.email"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">立即注册</el-button>
      <el-button @click="back">返回</el-button>
      <br>
      <br>
      <span>已有账号，</span><router-link to="/login">立即登录</router-link>
    </el-form-item>
  </el-form>

</template>

<script>
export default {
  name: "register",
  data: function () {
    return {
      form: {
        username: '',
        password: '',
        studentNo: '',
        studentName: '',
        gender: '',
        email: '',
      }
    }
  },
  methods: {
    onSubmit() {
      let userinfo = {
        "username": this.form.username,
        "studentNo": this.form.studentNo,
        "studentName": this.form.studentName,
        "gender": this.form.gender,
        "email": this.form.email,
        "password": this.form.password
      }

      this.$axios.post("http://localhost:8090/user/register",userinfo)
      .then(res => {
        if (res.data.code == 200) {
          alert("注册成功")
          this.$router.push("login")
        } else if (res.data.code == 500) {
          alert("该学号已被注册，请直接登录");
        } else {
          alert(res.data.message);
        }
      })
    },

    back:function (){
      this.$router.go(-1)
    },
  }
}
</script>

<style scoped>
  el-form-item{
    width: 200px;
  }
</style>